/*
   This file is part of the 'Let's Do Stuff Together' project
   http://code.google.com/p/dostufftogether/

   Copyright 2010 Christoph Fuchs, Stefan Thaler

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
 */
package org.sti2.letsdostuff.location;

import java.util.ArrayList;

import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.MotionEvent;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapView;
import com.google.android.maps.OverlayItem;

/**
 * ItemizedOverlay implementation to show markers on a MapView. This class
 * handles the onTouchEvent which creates the markers.
 * 
 * @author Christoph 'Gigi' Fuchs
 * 
 */
public class ClickableItemizedOverlay extends ItemizedOverlay<OverlayItem> {

	private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();

	/**
	 * Creates a new itemized overlay. Use a pin-like graphic to get the correct
	 * position of the Drawable.
	 * 
	 * @param defaultMarker the default marker to use
	 */
	public ClickableItemizedOverlay(Drawable defaultMarker) {
		super(boundCenterBottom(defaultMarker));
	}

	/* (non-Javadoc)
	 * @see com.google.android.maps.ItemizedOverlay#createItem(int)
	 */
	@Override
	protected OverlayItem createItem(int i) {
		 return getOverlays().get(i);
	}
	
	/**
	 * Adds an overlay item to the list of overlay items.
	 * @param overlay the overlay item
	 */
	public void addOverlay(OverlayItem overlay) {
		getOverlays().add(overlay);
	    populate();
	}
	
	/* (non-Javadoc)
	 * @see com.google.android.maps.ItemizedOverlay#size()
	 */
	@Override
	public int size() {
		return getOverlays().size();
	}
	
	/* (non-Javadoc)
	 * @see com.google.android.maps.ItemizedOverlay#onTouchEvent(android.view.MotionEvent, com.google.android.maps.MapView)
	 */
	@Override
	public boolean onTouchEvent(MotionEvent event, MapView mapView) {
		//---when user lifts his finger---
		
		Log.v("Map", "onTouch fired. MotionEvent: " + event.getAction());
		
        if (event.getAction() == MotionEvent.ACTION_UP) {
			GeoPoint p = mapView.getProjection().fromPixels((int) event.getX(),
					(int) event.getY());
			
			addSingleOverlay(mapView, p);
			
			return true;
        }           
        
        return false;
	}

	/**
	 * Adds a single GeoPoint as a marker to the provided <code>MapView</code>.
	 * 
	 * @param mapView
	 *            the <code>MapView</code> that should hold the marker overlay
	 * @param geoPoint
	 *            the place where the marker should be
	 */
	public void addSingleOverlay(MapView mapView, GeoPoint geoPoint) {
		double lat = geoPoint.getLatitudeE6() / 1E6;
		double longitude = geoPoint.getLongitudeE6() / 1E6;

		// clear all markers first (easy solution)
		getOverlays().clear();
		
		OverlayItem marker = new OverlayItem(geoPoint, "", "");
		addOverlay(marker);
		Log.v("Map", "Added marker at " + lat + ", " + longitude);
	}

	/**
	 * 
	 * Gets the overlay objects. Since we only need one Overlay this list should
	 * only contain one element.
	 * 
	 * @return a list of overlay items
	 */
	public ArrayList<OverlayItem> getOverlays() {
		return mOverlays;
	}

}
